#Include colorGui.ahk
#Include chooseGui.ahk
class mianGui extends Gui {
    __New() {
        this.career := ""
        super.__New(, "LOTB取色宏")
        this.SetFont("s11", "微软雅黑")
        this.Opt("AlwaysOnTop")
        ; 创建按钮
        this.Add("Button", "x15 y20 w80 h40 vbtnWarlock", "咒术师")
        this.Add("Button", "y+10 wp hp vbtnBerserker", "力士")
        this.Add("Button", "y+10 wp hp vbtnQiGong", "气功")
        this.Add("Button", "y+10 wp hp vbtnAssassin", "刺客")
        this.Add("Button", "y+10 wp hp vsetting", "设置")
        ; 创建tap控件
        this.tap := this.Add("Tab2", "ys h0", ["1", "2", "3", "4", "5", "6", "7", "8"])
        this.tap.Opt("Bottom Buttons")
        ;-------------------咒术师Tab---------------------
        this.tap.UseTab(1)
        this.zsIsEnable := this.Add("Checkbox", "x110 y+ -Wrap vzsIsEnable", "启用")
        this.warlockLv := this.Add("ListView", "x110 y+10 r10 w500 NoSortHdr -Multi Center", ['技能名称', '按键', '类型', '参数值',
            '技能说明'])
        this.warlockLv.Add(, '死亡降临', 'v', '取色', '', '先释放技能，取技能图标12点方向偏左灰色部分')
        this.warlockLv.Add(, '黑炎龙', '4', '取色', '', '技能为灰色时取4数字左边一点')
        this.warlockLv.Add(, '破裂', 'f', '取色', '', '取F出现时F字母上任意部分')
        this.warlockLv.Add(, '夜叉', 't', '取色', '', '取字母R下来一点点')
        this.warlockLv.Add(, '修罗', 't', '取色', '', '与夜叉同一位置')
        this.warlockLv.Add(, '戒行锁', '2', '取色', '', '技能冷却完立即释放')
        this.warlockLv.Add(, '真言', 'r', '取色', '', '自动释放')
        this.warlockLv.Add(, "技能派系", , "派系", "暗系", "目前仅支持暗系")
        this.warlockLv.Add(, '插入延迟', , "设置", "是", "延长技能释放时间间隔")

        this.Add("Button", "x110 y+10 vbtnZsSave", "保存配置")
        this.Add("Button", "x+10 vbtnZsLoad", "加载配置")
        ;-------------------力士Tab---------------------
        this.tap.UseTab(2)
        this.lsIsEnable := this.Add("Checkbox", "x110 y+ -Wrap vlsIsEnable", "启用")
        this.BerserkerkLv := this.Add("ListView", "x110 y+10 r10 w500 NoSortHdr -Multi Center", ['技能名称', '按键', '类型', '参数值',
            '技能说明'])
        this.BerserkerkLv.Add(, '削骨', 'r', '取色', '', '取技能图标L左边一点点')
        this.BerserkerkLv.Add(, '怒扫', 'r', '取色', '', '同上，最好坐标一致')
        this.BerserkerkLv.Add(, '殆灭', 'f', '取色', '', '殆灭出现后，取F旁边的颜色')
        this.BerserkerkLv.Add(, '削骨延时', 't', '延时', '120', '延时设置默认：120ms')
        this.BerserkerkLv.Add(, '毁灭延时', 'r', '延时', '70', '延时设置默认：70ms')
        this.BerserkerkLv.Add(, '启用殆灭', "f", "设置", "是", "需要右键秘籍，否则无法使用")
        this.BerserkerkLv.Add(, "技能派系", , "派系", "暗系", "选择力士的技能派系")

        this.Add("Button", "x110 y+10 vbtnLsSave", "保存配置")
        this.Add("Button", "x+10 vbtnLsLoad", "加载配置")
        ;-------------------气功Tab---------------------
        this.tap.UseTab(3)
        this.qgIsEnable := this.Add("Checkbox", "x110 y+ -Wrap vqgIsEnable", "启用")
        this.forceMasterLv := this.Add("ListView", "x110 y+10 r10 w500 NoSortHdr -Multi Center", ['技能名称', '按键', '类型', '参数值',
            '技能说明'])
        this.forceMasterLv.Add(, '觉醒双龙破', 'f', '取色', '', '测试测试测试')
        this.forceMasterLv.Add(, '火莲掌', 'x', '取色', '', '先释放技能，取技能图标灰色部分')
        this.forceMasterLv.Add(, '莲花指', 'c', '取色', '', '取技能图标为灰色部分')
        this.forceMasterLv.Add(, '流星指', 'v', '取色', '', '只要F键出现即按下，会出现捡东西，野区复活他人动作')
        this.forceMasterLv.Add(, '玄冰掌', 'f', '取色', '', '测试测试测试')
        this.forceMasterLv.Add(, '雪冰掌', 'z', '取色', '', '测试测试测试')
        this.forceMasterLv.Add(, '冰河掌', 'x', '取色', '', '技能冷却完立即释放')
        this.forceMasterLv.Add(, '内心值', , '取色', '', '无内时自动释放')
        this.forceMasterLv.Add(, "技能派系", , "派系", "火系", "选择气功的技能派系")
        this.forceMasterLv.Add(, '技能延迟', , "设置", "是", "延长技能释放时间间隔")

        this.Add("Button", "x110 y+10 vbtnQgSave", "保存配置")
        this.Add("Button", "x+10 vbtnQgLoad", "加载配置")
        ;-------------------刺客Tab---------------------
        this.tap.UseTab(4)
        this.ckIsEnable := this.Add("Checkbox", "x110 y+ -Wrap vckIsEnable", "启用")
        this.assassinLv := this.Add("ListView", "x110 y+10 r10 w500 NoSortHdr -Multi Center", ['技能名称', '按键', '类型', '参数值',
            '技能说明'])
        this.assassinLv.Add(, "技能派系", , "派系", "暗刺", "选择刺客的技能派系")
        this.assassinLv.Add(, '技能延迟', , "设置", "是", "延长技能释放时间间隔")

        this.Add("Button", "x110 y+10 vbtnCkSave", "保存配置")
        this.Add("Button", "x+10 vbtnCkLoad", "加载配置")
        ;-------------------设置Tab---------------------
        this.tap.UseTab(5)
        this.Add("Text", "x110 y+ h30", "长按热键")
        this.longPress := this.Add("DropDownList", "x+10 w70 vlongPress", ['侧键下', '侧键上', '左键', '右键', '中键'])
        this.Add("Text", "x+80 h30", "短按热键")
        this.shortPress := this.Add("DropDownList", "x+10 w70 vshortPress", ['侧键下', '侧键上', '左键', '右键', '中键'])

        this.Add("Text", "x110 y+20 h30", "游戏快捷键设置：")
        this.Add("Text", "x110 y+20 h30", "左键按键")
        this.leftPress := this.Add("Hotkey", "x+10 w70 Limit128 vleftPress Center", "r")
        this.Add("Text", "x+80 h30", "右键按键")
        this.rightPress := this.Add("Hotkey", "x+10 w70 Limit128 vrightPress Center", "t")

        this.Add("Button", "x110 y+50 vbtnSaveSet", "保存设置")
        this.Add("Button", "x+10 vbtnRead", "读取配置")

        ;-------------------按钮点击事件注册---------------------
        this.OnEvent("Close", (*) => ExitApp())
        this["btnWarlock"].OnEvent("Click", (*) => this.getCareer(1, this["btnWarlock"]))
        this["btnBerserker"].OnEvent("Click", (*) => this.getCareer(2, this["btnBerserker"]))
        this["btnQiGong"].OnEvent("Click", (*) => this.getCareer(3, this["btnQiGong"]))
        this["btnAssassin"].OnEvent("Click", (*) => this.getCareer(4, this["btnAssassin"]))
        this["setting"].OnEvent("Click", (*) => this.getCareer(5))

        ; 表格双击事件注册
        this.warlockLv.OnEvent("DoubleClick", (*) => this.setLvData())
        this.BerserkerkLv.OnEvent("DoubleClick", (*) => this.setLvData())
        this.forceMasterLv.OnEvent("DoubleClick", (*) => this.setLvData())
        this.assassinLv.OnEvent("DoubleClick", (*) => this.setLvData())
        ; 保存按钮事件注册
        this["btnZsSave"].OnEvent("Click", (*) => this.saveSkillDataToIni(this.warlockLv))
        this["btnLsSave"].OnEvent("Click", (*) => this.saveSkillDataToIni(this.BerserkerkLv))
        this["btnQgSave"].OnEvent("Click", (*) => this.saveSkillDataToIni(this.forceMasterLv))
        this["btnCkSave"].OnEvent("Click", (*) => this.saveSkillDataToIni(this.assassinLv))
        ; 加载按钮事件注册
        this["btnZsLoad"].OnEvent("Click", (*) => this.loadSkillDataFromIni(this.warlockLv, "咒术师"))
        this["btnLsLoad"].OnEvent("Click", (*) => this.loadSkillDataFromIni(this.BerserkerkLv, "力士"))
        this["btnQgLoad"].OnEvent("Click", (*) => this.loadSkillDataFromIni(this.forceMasterLv, "气功"))
        this["btnCkLoad"].OnEvent("Click", (*) => this.loadSkillDataFromIni(this.assassinLv, "刺客"))

        ; 系统设置按钮事件注册
        this["btnSaveSet"].OnEvent("Click", (*) => this.saveSysIni())
        this["btnRead"].OnEvent("Click", (*) => this.loadSysIni())

        ; 发送鼠标按钮或鼠标滚轮事件到控件, 即第一次默认点击咒术师按钮
        ControlClick(this["btnWarlock"].hwnd)
    }
    ; 读取职业名称
    getCareer(tapNum, btn := "") {
        this.tap.Choose(tapNum)
        if (btn != "") {
            this.career := btn.Text
            global careerData := this.getCareerData()
        }

    }
    /**
     * 获取职业数据
     * @returns {Object} 返回职业表格名称与技能派系数组
     */
    getCareerData() {
        switch this.career {
            case "咒术师":
                arrBuild := ["暗系", "冰系"]
                return { lv: this.warlockLv, careerBuild: arrBuild }
            case "力士":
                arrBuild := ["暗系", "地系"]
                return { lv: this.BerserkerkLv, careerBuild: arrBuild }
            case "气功":
                arrBuild := ["火系", "冰系"]
                return { lv: this.forceMasterLv, careerBuild: arrBuild }
            case "刺客":
                arrBuild := ["暗刺", "隐刺"]
                return { lv: this.assassinLv, careerBuild: arrBuild }
        }
    }
    ; 获取职业技能对象
    getCareeSkill() {
        careerSkill := ""
        switch this.career {
            case "咒术师":
                if this.zsIsEnable.Value
                    careerSkill := warlock(this.warlockLv)
                else
                    MsgBox("咒术师技能未启用！", "提示", "16 Owner" this.Hwnd)
            case "力士":
                if (this.lsIsEnable.Value)
                    careerSkill := berserkerk(this.BerserkerkLv)
                else
                    MsgBox("力士技能未启用！", "提示", "16 Owner" this.Hwnd)
            case "气功":
                if (this.qgIsEnable.Value)
                    careerSkill := forceMaster(this.forceMasterLv)
                else
                    MsgBox("气功技能未启用！", "提示", "16 Owner" this.Hwnd)
            case "刺客":
                if (this.ckIsEnable.Value)
                    careerSkill := assassin(this.assassinLv)
                else
                    MsgBox("刺客技能未启用！", "提示", "16 Owner" this.Hwnd)
        }
        return careerSkill
    }
    ; 保存技能参数至配置文件
    saveSkillDataToIni(lv) {
        Pairs := ""
        loop lv.GetCount() {
            Pairs .= lv.GetText(A_Index, 1) "=" lv.GetText(A_Index, 4) "`n"
        }
        IniWrite(Pairs, iniPath, this.career)
        MsgBox("技能配置保存成功！", "提示", "64 Owner" this.Hwnd)
    }
    ; 加载技能参数
    loadSkillDataFromIni(lv, careerName) {
        if (!FileExist(iniPath) or !iniData := IniRead(iniPath, careerName, , 0) or lv == "") {
            MsgBox("配置文件无" this.career "技能取色参数或数据错误！", "提示", "16 Owner" this.Hwnd)
            return
        }
        ; iniData := IniRead(iniPath, careerName)
        if (iniData != "") {
            loop parse iniData, '`n', '`r' {
                if (A_LoopField == "")
                    return
                dataArray := StrSplit(A_LoopField, '=') ; 读取每行的数据
                Lv.Modify(A_Index, "Col4", dataArray[2]) ; 写入参数值
            }
            Lv.ModifyCol(4)
            iniData := ""
        }
    }
    ; 保存系统设置至配置文件
    saveSysIni() {
        Pairs := "longPress=" getHotkeyValue(this.longPress.Text)
            . "`nshortPress=" getHotkeyValue(this.shortPress.Text)
            . "`nleftPress=" this.leftPress.Value
            . "`nrightPress=" this.rightPress.Value
        IniWrite(Pairs, iniPath, "SysSet")
        MsgBox("设置保存成功！", "提示", "64 Owner" this.Hwnd)
    }
    ; 读取系统设置
    loadSysIni() {
        global careerData
        if (!IniRead(iniPath, "SysSet", , 0)) {
            MsgBox("配置文件无系统设置参数！", "提示", "16 Owner" this.Hwnd)
            return
        }
        this.longPress.Text := getHotkeyName(IniRead(iniPath, "SysSet", "longPress"))
        this.shortPress.Text := getHotkeyName(IniRead(iniPath, "SysSet", "shortPress"))
        this.leftPress.Text := IniRead(iniPath, "SysSet", "leftPress")
        this.rightPress.Text := IniRead(iniPath, "SysSet", "rightPress")
        this.loadSkillDataFromIni(careerData.lv, this.career)
    }
    ; 设置表格数据
    setLvData(*) {
        global color, careerData
        ; 职业数据
        global rowNum := careerData.lv.getNext(, "F")
        if (!rowNum)
            return
        else if (careerData.lv.GetText(rowNum, 3) == "取色") {
            ;打开取色窗口，隐藏主窗口
            mainGuiShoworHide()
            color.skill.Text := careerData.lv.GetText(rowNum, 1)
            ; 图片控件显示对应技能图标
            ; try color.skillPic.Value := ""
        } else if (careerData.lv.GetText(rowNum, 3) == "延时") {
            choose := chooseGui(main.career, careerData.lv.GetText(rowNum, 1), careerData.lv, rowNum, , careerData.lv.GetText(rowNum, 5))
            choose.timeDelay.Visible := true
            choose.timeDelay.Text := careerData.lv.GetText(rowNum, 4)
            choose.showGui()
        } else if (careerData.lv.GetText(rowNum, 3) == "设置") {
            choose := chooseGui(main.career, careerData.lv.GetText(rowNum, 1), careerData.lv, rowNum, , careerData.lv.GetText(rowNum, 5))
            choose.ddlData.Visible := true
            choose.ddlData.Text := careerData.lv.GetText(rowNum, 4)
            choose.showGui()
        } else if (careerData.lv.GetText(rowNum, 3) == "派系") {
            choose := chooseGui(main.career, careerData.lv.GetText(rowNum, 1), careerData.lv, rowNum, careerData.careerBuild, careerData.lv.GetText(rowNum, 5))
            choose.ddlData.Visible := true
            choose.ddlData.Text := careerData.lv.GetText(rowNum, 4)
            choose.showGui()
        }
    }
    openChosseGui(ctrName, Arraydata := ["是", "否"]) {
        choose := chooseGui(this.career, careerData.lv.GetText(rowNum, 1), careerData.lv, rowNum, Arraydata, careerData.lv.GetText(rowNum, 5))
        choose.ctrName.Visible := true
        choose.ctrName.Text := careerData.lv.GetText(rowNum, 4)
        choose.showGui()
    }
    ; 主窗口初始化
    Initialization() {
        this.loadSysIni()
        this.Show()
    }
}